package com.fa.controller;


import com.alibaba.excel.EasyExcel;
import com.fa.common.annotation.RateLimiter;
import com.fa.common.annotation.RepeatSubmit;
import com.fa.common.core.controller.BaseController;
import com.fa.common.core.domain.AjaxResult;

import com.fa.common.core.page.TableDataInfo;
import com.fa.domain.Warehouse;
import com.fa.domain.WarehouseSearchVO;
import com.fa.domain.WarehouseVO;
import com.fa.listener.UploadDataListener;
import com.fa.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wm/warehouse")
public class WarehouseController extends BaseController {

    @Autowired
    private WarehouseService warehouseService;
    @RateLimiter(time = 50, count = 50)
    @GetMapping("/list")
    public TableDataInfo warehouseList(WarehouseSearchVO warehouseSearchVO){
        startPage();
        List<WarehouseVO> warehouseVOS = warehouseService.warehouseList(warehouseSearchVO);
        return getDataTable(warehouseVOS);
    }
    @RateLimiter(time = 50, count = 50)
    @GetMapping("/listById")
    public AjaxResult getWarehouseById(Long warehouseId){
        WarehouseVO warehouseById = warehouseService.getWarehouseById(warehouseId);
        return success(warehouseById);
    }
    @RateLimiter(time = 50, count = 50)
    @GetMapping("/del")
    public AjaxResult delWarehouse(@RequestParam List<Integer> warehouseIds){
        warehouseService.delWarehouse(warehouseIds);
        return success();
    }

    @RepeatSubmit(interval = 1000)
    @PostMapping("/update")
    public AjaxResult updateWarehouse(@RequestBody @Validated Warehouse warehouse){
        warehouseService.updateWarehouse(warehouse);
        return success();
    }


    @RepeatSubmit(interval = 1000)
    @PostMapping("/add")
    public AjaxResult addWarehouse(@RequestBody @Validated Warehouse warehouse) throws Exception {
        warehouseService.addWarehouse(warehouse);
        return success();
    }

    @PostMapping("/export")
    public void warehouseExport(HttpServletResponse response, WarehouseSearchVO warehouseSearchVO) throws Exception {
        List<WarehouseVO> warehouse;
        if (warehouseSearchVO.isLoad() && warehouseSearchVO.isLoadTemplate()){
             warehouse = new ArrayList<>();
        }else if (warehouseSearchVO.isLoad()){
             warehouse = warehouseService.warehouseList(warehouseSearchVO);
        }else {
            return;
        }
        /*ExcelUtil<WarehouseVO> util = new ExcelUtil<>(WarehouseVO.class);
        util.exportExcel(response, warehouse, "仓库信息");*/
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WarehouseVO.class).sheet("仓库信息").doWrite(warehouse);
    }

    @PostMapping("/import")
    public AjaxResult warehouseImport(MultipartFile file) throws IOException {
        /*ExcelUtil<Warehouse> util = new ExcelUtil<>(Warehouse.class);
        List<Warehouse> warehouseList = util.importExcel(file.getInputStream());
        warehouseService.addWarehouseList(warehouseList);*/
        EasyExcel.read(file.getInputStream(), Warehouse.class, new UploadDataListener(warehouseService)).sheet().doRead();
        return success();
    }



}
